<?php
namespace Home\Model;
use Think\Model\RelationModel;

class ClassModel extends RelationModel{
    protected $_link = array(
        "question_set"=>array(
            "mapping_type"=>3,
            "class_name"=>"question_set",
            "mapping_name"=>"question_set",
            "foreign_key"=>"class_cla_id",
            "parent_key"=>"cla_id"
        ),
    );

    protected $_validate = array(
        //新增班级的自动验证
        array("cla_name","require","班级名称不能为空!",1,"regex",3),
        array("cou_id","number","课程id必须是正整数!",1,"regex",1),
        array("cla_code","require","班级代码必须有!",1,"regex",1),
        array("cla_start",10,"开始时间要格式要正确!",1,"length",3),
        array("cla_end",10,"开始时间要格式要正确!",1,"length",3),
        array("cla_start,cla_end","checkTime","结束时间要大于开始时间而且开启使用要小于1年!",1,"callback",3),
        array("is_auto",array(0,1),"是否启用有误!",1,"in",3),
        //修改班级,验证一下id
        array("cla_id","number","班级id必须是正整数!",1,"regex",2),
        array("is_filed",array(0,1),"归档处理只有归档或不归档!",1,"in",2),
        //验证code是否重复4
        array("cla_code","","班级代码必须唯一!",1,"unique",4),
    );

    protected $_auto = array(
        array("cla_stu_num",0,1),       //新增班级的时候自动填充
        array("is_filed",0,1),
    );
    protected function checkTime($time){
        $start = strtotime($time["cla_start"]);
        $end = strtotime($time["cla_end"]);
        if($end<$start || $end-$start>31536000){
            return false;
        }
        return true;
    }
}